FROM golang:1.20

# 设置工作目录
WORKDIR /home/tmp

# 确认 /etc/apt/sources.list 存在
RUN if [ ! -f /etc/apt/sources.list ]; then \
      echo "deb http://deb.debian.org/debian buster main" > /etc/apt/sources.list; \
    fi

# 更换APT源为USTC镜像
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
# RUN echo 'deb http://mirrors.aliyun.com/debian jessie main' >> /etc/apt/sources.list
# RUN echo 'deb http://mirrors.aliyun.com/debian jessie-updates main' >> /etc/apt/sources.list
# 进行key配置
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B7B3B788A8D3785C
# 安装MySQL客户端
RUN apt-get update && apt-get install -y lsb-release && \
    wget https://repo.mysql.com//mysql-apt-config_0.8.24-1_all.deb && \
    export DEBIAN_FRONTEND=noninteractive && \
    dpkg -i mysql-apt-config_0.8.24-1_all.deb && \
    apt-get update && apt-get install -y mysql-client && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# 设置应用的工作目录
WORKDIR /home/app

# 复制 go.mod 和 go.sum 文件
COPY go.mod go.sum ./

# 配置Go环境并安装 bee 工具
RUN go env -w GO111MODULE=on && go env -w GOPROXY=https://goproxy.cn
RUN go install github.com/beego/bee/v2@latest && go mod download && go mod verify

# 复制应用代码
COPY . .

# 设置MySQL连接环境变量
ARG MYSQL_HOST=host.docker.internal
ARG MYSQL_PORT=3306
ENV MYSQL_HOST=${MYSQL_HOST}
ENV MYSQL_PORT=${MYSQL_PORT}

# 处理 SERVER_IP 参数
ARG SERVER_IP
RUN if [ -n "$SERVER_IP" ]; then python3 env.py $SERVER_IP; fi

# 启动命令
CMD ["bee", "run"]
